#!/usr/bin/env python3
"""
创建测试用户
"""

import asyncio
from sqlalchemy.ext.asyncio import AsyncSession
from app.core.database import engine, AsyncSessionLocal
from app.models.user import User
from app.core.security import get_password_hash

async def create_test_user():
    """创建测试用户"""
    async with AsyncSessionLocal() as session:
        # 检查用户是否已存在
        from sqlalchemy import select
        result = await session.execute(select(User).where(User.username == "testuser"))
        existing_user = result.scalar_one_or_none()
        
        if existing_user:
            print(f"测试用户已存在: ID={existing_user.id}, username={existing_user.username}")
            return existing_user.id
        
        # 创建新用户
        test_user = User(
            email="test@example.com",
            username="testuser",
            full_name="Test User",
            hashed_password=get_password_hash("testpassword123"),
            is_active=True,
            is_superuser=False
        )
        
        session.add(test_user)
        await session.commit()
        await session.refresh(test_user)
        
        print(f"创建测试用户成功: ID={test_user.id}, username={test_user.username}")
        return test_user.id

if __name__ == "__main__":
    asyncio.run(create_test_user())